package com.xjc.business.controller.schadmin;

import com.xjc.business.entity.po.schadmin.PersonPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.schadmin.BuildingRestAdminVo;
import com.xjc.business.entity.vo.schadmin.CollegeRestAdminVo;
import com.xjc.business.entity.vo.schadmin.PersonVo;
import com.xjc.business.service.BuildingAdminsService;
import com.xjc.business.service.CollageAdminRelationsService;
import com.xjc.business.service.CollegeAdminsService;
import com.xjc.business.service.DoorAdminsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-01-27 18:08
 */
@RestController("sys_person_controller")
@RequestMapping("/sch_admin/person")
public class PersonController {

    @Autowired
    CollegeAdminsService collegeAdminsService;

    @Autowired
    BuildingAdminsService buildingAdminsService;

    @Autowired
    DoorAdminsService doorAdminsService;

    /**
     * 分页查询人员信息
     * @param pageSize 每页显示条数
     * @param currentPage  当前页码
     * @param adminUserName 用户名
     * @param adminNickName 昵称
     * @param type 类型
     * @return 信息
     */
    @RequestMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<PersonVo>> getPersonPage(@PathVariable(value = "pageSize",required = false) Integer pageSize,@PathVariable(value = "currentPage",required = false) Integer currentPage, String adminUserName, String adminNickName, Integer type,Integer schoolId) {
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }

        if(type == null || schoolId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        switch (type){
            case 0:
                return collegeAdminsService.getCollegeAdminPage(pageSize,currentPage,adminUserName,adminNickName,schoolId);
            case 1:
                return buildingAdminsService.getBuildingAdminPage(pageSize,currentPage,adminUserName,adminNickName,schoolId);
            case 2:
                return doorAdminsService.getDoorAdminPage(pageSize,currentPage,adminUserName,adminNickName,schoolId);
        }

        throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
    }

    /**
     * 添加管理员
     * @param personPo 管理员信息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addPerson(@RequestBody(required = false) PersonPo personPo){

        if(personPo == null || personPo.getType() == null || personPo.getSchoolId() == null ||  personPo.getUsername() == null || personPo.getPassword() == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        switch (personPo.getType()){
            case 0:
                return collegeAdminsService.addCollegeAdmin(personPo);
            case 1:
                return buildingAdminsService.addBuildingAdmin(personPo);
            case 2:
                return doorAdminsService.addDoorAdmin(personPo);
        }

        throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
    }

    /**
     * 获取学校中空闲的学院管理员列表
     * @param schoolId 学校id
     * @return 信息
     */
    @GetMapping("/rest_college_admin_list/{schoolId}")
    public Result<List<CollegeRestAdminVo>> getRestCollegeAdminList(@PathVariable(value = "schoolId",required = false) Integer schoolId){
        if(schoolId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return collegeAdminsService.getRestCollegeAdminList(schoolId);
    }

    /**
     * 获取学校中空闲的楼宇管理员列表
     * @param schoolId 学校id
     * @return 信息
     */
    @GetMapping("/rest_building_admin_list/{schoolId}")
    public Result<List<BuildingRestAdminVo>> getRestBuildingAdminList(@PathVariable(value = "schoolId",required = false) Integer schoolId){
        if(schoolId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return buildingAdminsService.getRestBuildingAdminList(schoolId);
    }
}
